import store from '@/store'
// import Axios from '@/service';
// import router from '@/router'
// import Config from '@/public/config'
// import {
//   Toast
// } from 'vant'
// import config from './config';
// import { Store } from 'vuex';
// import router from '@/router'
export default {

  // 日期格式转换--时间字符串转化成视觉格式
  // Add by zgf, Updated on 2018/9/4
  // 使用示例：this.$util.formatDate('/Date(1435646188067+0800)/','yyyy-MM-dd hh:mm:ss')
  // 使用示例：this.$util.formatDate('2018-12-12T12:12:12','yyyy-MM-dd hh:mm:ss')
  // 使用示例：this.$util.formatDate('2018-12-12 12:12:12','yyyy/MM/dd hh:mm:ss')
  // 使用示例：this.$util.formatDate(new Date(),'yyyy/MM/dd hh:mm:ss')
  // 作为方法调用：this.$util.formatDate(时间,[格式])
  // 作为filter调用：{{时间 | formatDate([格式])}}
  formatDate: function (v, format) {
    if (!v) return "";
    var d = v;
    if (typeof v === 'string') {
      if (v.indexOf("/Date(") > -1)
        d = new Date(parseInt(v.replace("/Date(", "").replace(")/", ""), 10));
      else
        d = new Date(Date.parse(v.replace(/-/g, "/").replace("T", " ").split(".")[0])); //.split(".")[0] 用来处理出现毫秒的情况，截取掉.xxx，否则会出错
    }
    var o = {
      "M+": d.getMonth() + 1,
      "d+": d.getDate(),
      "h+": d.getHours(),
      "m+": d.getMinutes(),
      "s+": d.getSeconds(),
      "q+": Math.floor((d.getMonth() + 3) / 3),
      "S": d.getMilliseconds()
    };
    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
      if (new RegExp("(" + k + ")").test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
      }
    }
    return format;
  },

  //时间格式数据转化成 刚刚 n分钟前 n小时前 今天 hh:mm 昨天 hh:mm MM/dd hh:mm yyyy/MM/dd hh:mm
  //时间格式可以是 时间对象；yyyy/MM/dd hh:mm:ss;yyyy-MM-dd hh:mm:ss;'/Date(1435646188067+0800)/';'2018-12-12T12:12:12';等任意合法的时间格式
  formatTimestamp: function (v) {
    let timeStr = this.formatDate(v, 'yyyy/MM/dd hh:mm:ss')
    let timestamp = parseInt(new Date(timeStr).getTime() / 1000);

    function zeroize(num) {
      return (String(num).length == 1 ? '0' : '') + num;
    }
    var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
    var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数

    var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
    var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象

    var Y = tmDate.getFullYear(),
      m = tmDate.getMonth() + 1,
      d = tmDate.getDate();
    var H = tmDate.getHours(),
      i = tmDate.getMinutes(),
      s = tmDate.getSeconds();

    if (timestampDiff < 60) { // 一分钟以内
      return "刚刚";
    } else if (timestampDiff < 3600) { // 一小时前之内
      return Math.floor(timestampDiff / 60) + "分钟前";
    } else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
      return '今天 ' + zeroize(H) + ':' + zeroize(i);
    } else {
      var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
      if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
        return '昨天 ' + zeroize(H) + ':' + zeroize(i);
      } else if (curDate.getFullYear() == Y) {
        return zeroize(m) + '/' + zeroize(d) + ' ' + zeroize(H) + ':' + zeroize(i);
      } else {
        // return Y + '/' + zeroize(m) + '/' + zeroize(d) + ' ' + zeroize(H) + ':' + zeroize(i);
        return Y + '/' + zeroize(m) + '/' + zeroize(d);
      }
    }
  },


  //计算两个时间的时间间隔
  //start 2019-12-12 12:12:12
  //end 2019-12-13 12:12:12
  //returnSeconds返回毫秒数，默认返回xx天xx时xx分
  timeDuration: function (start, end, returnSeconds) {
    start = this.formatDate(start, 'yyyy/MM/dd hh:mm:ss');
    end = this.formatDate(end, 'yyyy/MM/dd hh:mm:ss');
    let startTime = new Date(start); // 开始时间
    let endTime = new Date(end); // 结束时间
    let usedTime = endTime - startTime; // 相差的毫秒数
    if (returnSeconds) return usedTime;
    let days = Math.floor(usedTime / (24 * 3600 * 1000)); // 计算出天数
    let leavel = usedTime % (24 * 3600 * 1000); // 计算天数后剩余的时间
    let hours = Math.floor(leavel / (3600 * 1000)); // 计算剩余的小时数
    let leavel2 = leavel % (3600 * 1000); // 计算剩余小时后剩余的毫秒数
    let minutes = Math.floor(leavel2 / (60 * 1000)); // 计算剩余的分钟数
    return days + '天' + hours + '时' + minutes + '分';
  },
  //判断某年是否是闰年
  //2月平年28天，闰年29天
  isLeapYear(Year) {
    if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
      return (true);
    } else {
      return (false);
    }
  },
  // 延期几天
  getDelay(val) {
    // 计算是否已过期
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var date = d.getDate();
    var str = year + "/" + month + "/" + date;
    var strTime = new Date(str); // 当前时间
    val = new Date(val);
    var usedTime = val - strTime; // 相差的毫秒数
    var res = 0;
    if (usedTime < 0) {
      //任务截止时间小于当前时间  已过期
      res = Math.floor(usedTime / (24 * 3600 * 1000)); // 计算出天数
      res = Math.abs(res);
    }
    return res
  },

  //对象深拷贝
  deepClone: function (obj) {
    if (!(obj instanceof Object)) return obj;
    return JSON.parse(JSON.stringify(obj));
  },

  //获取当前登录用户信息
  getUser: function () {
    var userInfo = localStorage.getItem('jUser');
    if (userInfo) {
      return JSON.parse(localStorage.getItem('jUser'));
    } else {
      return null;
    }
  },

  //获取url上的参数
  getHashParameters: function () {
    if (location.hash.indexOf('?') == -1) {
      return {}
    }
    var arr = (location.hash || '').split('?')[1].split('&')
    var params = {}
    for (var i = 0; i < arr.length; i++) {
      var data = arr[i].split('=')
      if (data.length === 2) {
        params[data[0]] = data[1]
      }
    }
    return params
  },


  //是否是微信中打开
  isWeixin: function () {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
      return true;
    } else {
      return false;
    }
  },


  //数组倒叙排列
  arrayReverse: function (arr) {
    if (!(arr instanceof Array)) return []
    let newArr = []
    for (var i in arr) {
      let item = arr[i]
      newArr.unshift(item)
    }
    return newArr
  },


  //比较两个时间大小
  compareDate(date1, date2) {
    let date1DateObj = new Date(this.formatDate(date1, 'yyyy/MM/dd'))
    let date2DateObj = new Date(this.formatDate(date2, 'yyyy/MM/dd'))
    let time1 = date1DateObj.getTime()
    let time2 = date2DateObj.getTime()
    if (time1 < time2) {
      return 1
    } else if (time1 == time2) {
      return 0
    } else {
      return -1
    }
  },

  //转换textarea里的换行符为br/
  transEnterToBr(str) {
    return str.replace(/\r/ig, "<br/>").replace(/\n/ig, "<br/>");
  },


  //将时间转化乘时间戳到秒
  //date : 2020-02-02 2020/02/02 new Date() 等
  dateToSeconds(date){
    if(!date) return 0
    let dateStr = this.formatDate(date,'yyyy/MM/dd hh:mm:ss')
    return new Date(dateStr).getTime()/1000 
  },

  //判断是否是移动设备
  isMobile(){
    return /Android|iPhone|iPad|iPod|BlackBerry|webOS|Windows Phone|SymbianOS|IEMobile|Opera Mini/i.test(navigator.userAgent)
  },

  //load script
  loadScript(url, callback) {
    var fn = callback || function () { }
    var script = null
    if(store.state.jsLoader.includes(url)){
      fn()
      return
    }else{
      script = document.createElement('script')
      script.type = 'text/javascript';
      if (script.readyState) {
        script.onreadystatechange = function () {
          if (script.readyState == 'loaded' || script.readyState == 'complete') {
            script.onreadystatechange = null;
            fn();
          }
        };
      } else {
        //其他浏览器
        script.onload = function () {
          fn();
        };
      }
      script.src = url;
      document.getElementsByTagName('head')[0].appendChild(script);
      store.commit('setJsLoader',url)
    }
  }




}